/**
* @Author: zou.yingbin
* @Create  : 2020/9/15 15:36
* @Modify  : 2020/9/15 15:36
 */

package main

import (
	"fmt"
	"io/ioutil"
	"strconv"
	"strings"
)

type uiData struct {
	menu map[string]func(args ...string)
}

// 初始化菜单
func (r *uiData) Init() {
	r.menu = make(map[string]func(args ...string))
	r.menu["order"] = r.SendOrder
	r.menu["orders"] = r.SendOrders
	r.menu["qryFund"] = r.QryFund
	r.menu["qryGoods"] = r.QryGoods
	r.menu["qryOrder"] = r.QryOrder
	r.menu["qryTrade"] = r.QryTrade
	r.menu["qryPosition"] = r.qryPosition
	r.menu["qryExchange"] = r.qryExchange

	r.menu["logout"] = func(args ...string) {
		LogoutAll()
	}
}

// 显示帮助
func ShowHelp() {
	fileName := "help.ini"
	b, err := ioutil.ReadFile(fileName)
	if err == nil {
		fmt.Println(string(b))
	}
}

// 显示下单对话框
func ShowOrderUI() {
	OrderDlg(OnOrderHandle)
}

func OnOrderHandle(d *stOrderUI) {
	fmt.Printf("%+v\n", d)

	if len(d.LoginId) == 0 {
		var goodsId uint32 = 0
		for _, s := range usrConn {
			if s.bLogin {
				if goodsId == 0 {
					goodsId = s.GetGoodsId(d.GoodsCode)
				}
				s.SendOrder(d.MarketID, goodsId, d.Qty, d.BuildType, d.BuyOrSell, d.OrderPrice)
			}
		}
	} else {
		cmd := strings.Split(d.LoginId, ",")

		// 去除空格、回车、换行
		for i, s := range cmd {
			s = strings.Replace(s, " ", "", -1)
			s = strings.Replace(s, "\r", "", -1)
			s = strings.Replace(s, "\n", "", -1)
			cmd[i] = s
		}

		var goodsId uint32 = 0
		for _, acc := range cmd {
			if s := GetSession(acc); s != nil {
				if goodsId == 0 {
					goodsId = s.GetGoodsId(d.GoodsCode)
				}
				s.SendOrder(d.MarketID, goodsId, d.Qty, d.BuildType, d.BuyOrSell, d.OrderPrice)
			}
		}
	}
}

// 与用户输入交互
func (r *uiData) Do() {
	r.Init()

	for true {
		var strIn string

		if _, err := fmt.Scanln(&strIn); err != nil {
			continue
		}

		if strIn == "help" {
			ShowHelp()
			continue
		} else if strIn == "orderui" {
			ShowOrderUI()
			continue
		}

		cmd := strings.Split(strIn, ",")

		// 去除空格、回车、换行
		for i, s := range cmd {
			s = strings.Replace(s, " ", "", -1)
			s = strings.Replace(s, "\r", "", -1)
			s = strings.Replace(s, "\n", "", -1)
			cmd[i] = s
		}

		if len(cmd) > 0 {
			f, ok := r.menu[cmd[0]]
			if ok {
				f(cmd[1:]...)
			}
		}
	}
}

// 发送委托单
func (r *uiData) SendOrder(args ...string) {
	if len(args) != 6 {
		fmt.Println("order param error")
		return
	}

	var goodsCode string
	var marketId, goodsId, qty, buildType, buyOrSell uint32 = 0, 0, 0, 0, 0

	// 解析参数
	if d, err := strconv.ParseUint(args[1], 10, 32); err == nil {
		marketId = uint32(d)
	} else {
		fmt.Println(err)
		return
	}

	if d, err := strconv.ParseUint(args[2], 10, 32); err == nil {
		goodsId = uint32(d)
	} else {
		goodsCode = args[2]
	}

	if d, err := strconv.ParseUint(args[3], 10, 32); err == nil {
		qty = uint32(d)
	} else {
		fmt.Println(err)
		return
	}

	if d, err := strconv.ParseUint(args[4], 10, 32); err == nil {
		buildType = uint32(d)
	} else {
		fmt.Println(err)
		return
	}

	if d, err := strconv.ParseUint(args[5], 10, 32); err == nil {
		buyOrSell = uint32(d)
	} else {
		fmt.Println(err)
		return
	}

	if s := GetSession(args[0]); s != nil {
		if goodsId == 0 {
			if goodsId = s.GetGoodsId(goodsCode); goodsId == 0 {
				fmt.Println("qryGoods info fail, please try it later")
				return
			}
		}
		s.SendOrder(marketId, goodsId, qty, buildType, buyOrSell, 0)
	} else {
		fmt.Println(args[0], " loginId not found")
	}
}

func (r *uiData) SendOrders(args ...string) {
	if len(args) != 5 {
		fmt.Println("orders param error")
		return
	}

	for _, s := range usrConn {
		if s.bLogin {
			tmp := make([]string, 0)
			tmp = append(tmp, s.strAcc)
			tmp = append(tmp, args...)
			r.SendOrder(tmp...)
		}
	}
}

// 通用查询模板
func QryCommon(f func(s *Session), args ...string) {
	if len(args) > 0 {
		for _, arg := range args {
			if s := GetSession(arg); s != nil {
				f(s)
			} else {
				fmt.Println(arg, " loginId not found")
			}
		}
	} else {
		for _, s := range usrConn {
			f(s)
		}
	}
}

// 查询资金
func (r *uiData) QryFund(args ...string) {
	QryCommon(func(s *Session) {
		s.qryFund()
	}, args...)
}

// 查询商品
func (r *uiData) QryGoods(args ...string) {
	QryCommon(func(s *Session) {
		s.qryGoods()
	}, args...)
}

// 查询委托单
func (r *uiData) QryOrder(args ...string) {
	QryCommon(func(s *Session) {
		s.qryOrder()
	}, args...)
}

// 查询成交
func (r *uiData) QryTrade(args ...string) {
	QryCommon(func(s *Session) {
		s.qryTrade()
	}, args...)
}

// 查询持仓
func (r *uiData) qryPosition(args ...string) {
	QryCommon(func(s *Session) {
		s.qryPosition()
	}, args...)
}

// 查询交易所
func (r *uiData) qryExchange(args ...string) {
	QryCommon(func(s *Session) {
		s.qryExchange()
	}, args...)
}
